// src/main/java/com/example/controller/SysDictController.java
package com.bpmn.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bpmn.common.annotation.ResponseInfoWrapper;
import com.bpmn.common.exception.ServiceException;
import com.bpmn.common.pojo.PageResult;
import com.bpmn.system.domain.SysDict;
import com.bpmn.system.domain.SysDictData;
import com.bpmn.system.service.ISysDictDataService;
import com.bpmn.system.service.ISysDictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author rain
 */
@Tag(name = "字典接口")
@RestController
@RequiredArgsConstructor
@ResponseInfoWrapper
@RequestMapping("/system/dict")
public class SysDictController {

    private final ISysDictService dictService;

    private final ISysDictDataService dictDataService;

    @GetMapping("/list")
    @Operation(summary = "获取字典列表")
    public PageResult<SysDict> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            SysDict dict) {
        IPage<SysDict> dictIPage = dictService.selectDictPage(new Page<>(pageNum, pageSize), dict);
        return new PageResult<>(dictIPage.getRecords(), dictIPage.getTotal());
    }

    @PostMapping
    @Operation(summary = "添加字典")
    public Boolean add(@RequestBody SysDict dict) {
        if (!dictService.checkDictTypeUnique(dict.getType())) {
            throw new ServiceException("字典类型已存在");
        }
        return dictService.save(dict);
    }

    @PutMapping
    @Operation(summary = "更新字典")
    public Boolean update(@RequestBody SysDict dict) {
        return dictService.updateById(dict);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除字典")
    public Boolean delete(@PathVariable Long id) {
        return dictService.removeById(id);
    }

    @GetMapping("/data/list")
    @Operation(summary = "获取字典数据列表")
    public PageResult<SysDictData> dataList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            SysDictData dictData) {
        IPage<SysDictData> sysDictDataIPage = dictDataService.selectDictDataPage(new Page<>(pageNum, pageSize), dictData);
        return new PageResult<>(sysDictDataIPage.getRecords(), sysDictDataIPage.getTotal());
    }

    @GetMapping("/data/type/{dictType}")
    @Operation(summary = "根据字典类型获取字典数据")
    public List<SysDictData> getDataByType(@PathVariable String dictType) {
        return dictDataService.selectDictDataByType(dictType);
    }

    @PostMapping("/data")
    @Operation(summary = "添加字典数据")
    public Boolean addData(@RequestBody SysDictData dictData) {
        return dictDataService.save(dictData);
    }

    @PutMapping("/data")
    @Operation(summary = "更新字典数据")
    public Boolean updateData(@RequestBody SysDictData dictData) {
        return dictDataService.updateById(dictData);
    }

    @DeleteMapping("/data/{id}")
    @Operation(summary = "删除字典数据")
    public Boolean deleteData(@PathVariable Long id) {
        return dictDataService.removeById(id);
    }
}